Skip to content

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript#10702

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks
Jul 17, 2026
Merged

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript#10702
copybara-service[bot] merged 1 commit into
google:masterfrom
destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks

Conversation

@destro4evr-rgb

@destro4evr-rgb destro4evr-rgb commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Addresses VRP report https://issuetracker.google.com/issues/533344097

Several graph-building functions in litert/tensor/arithmetic.h use axis values or buffer-derived indices as direct subscripts into heap-allocated std::vector shape vectors without validating that the values are within bounds. std::vector::operator[] is unchecked in release builds.

Concatenationaxis is passed directly by the caller and used as a subscript into the output and input shape vectors with no prior validation. A negative value implicitly casts to a SIZE_MAX-class size_t, directing the read and write gigabytes past the heap allocation.

ReduceMax, Mean, Sum — Each function reads axes from a tensor buffer as int32_t values and uses them directly as subscripts into the output shape vector (o_info.shape[b_data[0]], o_info.shape[b_data[i]]). A value of -1 in the buffer becomes SIZE_MAX after the implicit cast to size_t.

Transpose — The permutation buffer is read as int32_t values and used to index into input_shape inside a loop. A negative permutation value produces a SIZE_MAX-class index. Additionally, if the permutation buffer contains more entries than the input rank, the loop writes to output_info.shape[i] beyond the resized vector length.

Pad and PadV2 — The loop reads b_data[i * 2] and b_data[i * 2 + 1] for every dimension, but the buffer length is not checked against 2 * input_rank before the loop. A buffer shorter than 2 * input_rank elements causes reads past the buffer's allocation.

Fix

Validate axis/index values against [0, shape.size()) before any subscript, and return an ErrorTensor on failure. For Pad and PadV2, confirm the locked buffer holds at least 2 * o_info.shape.size() elements before entering the loop.

This follows the same pattern introduced in #10464 for ExpandDims, Pack, and OneHot.

Affected functions

Function Issue Fixed
Concatenation axis unchecked before shape[axis]
ReduceMax b_data[0] / b_data[i] unchecked before shape[b_data[*]]
Mean same as ReduceMax
Sum same as ReduceMax
Transpose perm_data[i] unchecked; perm length vs input rank unchecked
Pad buffer length not checked against 2 * input_rank before loop
PadV2 same as Pad

Precedent

PR #10464 ("Bounds-check data-derived axes and quantization params in tensor builder", merged 2026-07-02) fixed the same bug class in ExpandDims, Pack, and OneHot in the same file. The fix here applies the identical pattern to the remaining unchecked functions.

@destro4evr-rgb

Copy link
Copy Markdown
Contributor Author

@qukhan - flagging this for review. This PR addresses heap out-of-bounds reads in several litert/tensor/arithmetic.h graph-building functions (Concat, ReduceMax, Mean, Sum, Transpose, Pad) where attacker-controlled axis values or buffer indices are used as unchecked subscripts into shape vectors.

@qukhan

qukhan commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

There was a hiccup in our import flow, I think it should be good now.

copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 15, 2026
--
eac46c23a8d86466db45046a2153ad333802aa05 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Concatenation, ReduceMax, Mean, Sum, Transpose, Pad, and PadV2 each used
axis values or buffer-derived indices as direct subscripts into heap-allocated
std::vector shape vectors without validating that the values are within the
vector's bounds.  std::vector::operator[] is unchecked in release builds, so
a negative value (implicitly cast to a near-SIZE_MAX size_t) or an index
>= the vector size silently reads or writes past the heap allocation.

The fix follows the same pattern used in PR #10464 for ExpandDims, Pack, and
OneHot: validate the value against [0, shape.size()) before the subscript, and
return an ErrorTensor on failure.  For Pad and PadV2 the check confirms the
padding buffer holds at least 2 * input_rank elements before the loop that
reads b_data[i*2] and b_data[i*2+1].
FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks eac46c23a8d86466db45046a2153ad333802aa05
LiteRT-PiperOrigin-RevId: 946930488
@destro4evr-rgb

Copy link
Copy Markdown
Contributor Author

Hi @qukhan , both Copybara PRs (#10754 on XNNPACK and #8717 on LiteRT) have now completed all CI checks cleanly.
Could you take a look and merge when you get a chance?

@destro4evr-rgb
destro4evr-rgb force-pushed the fix/litert-arithmetic-axis-bounds-checks branch 2 times, most recently from 75a95ec to 23c5ae7 Compare July 16, 2026 14:28
@destro4evr-rgb

Copy link
Copy Markdown
Contributor Author

Hey @qukhan , rebased the PR onto current master to resolve the conflict from the LockedBufferSpan refactor. Should be clean now."

copybara-service Bot pushed a commit that referenced this pull request Jul 16, 2026
--
23c5ae7 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae7
PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 16, 2026
--
23c5ae781b34cbc55737c930ff7ba73423f0f715 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae781b34cbc55737c930ff7ba73423f0f715
LiteRT-PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 16, 2026
--
23c5ae781b34cbc55737c930ff7ba73423f0f715 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae781b34cbc55737c930ff7ba73423f0f715
LiteRT-PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 16, 2026
--
23c5ae781b34cbc55737c930ff7ba73423f0f715 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae781b34cbc55737c930ff7ba73423f0f715
LiteRT-PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 16, 2026
--
23c5ae781b34cbc55737c930ff7ba73423f0f715 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae781b34cbc55737c930ff7ba73423f0f715
LiteRT-PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit that referenced this pull request Jul 17, 2026
--
23c5ae7 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae7
PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 17, 2026
--
23c5ae781b34cbc55737c930ff7ba73423f0f715 by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 23c5ae781b34cbc55737c930ff7ba73423f0f715
LiteRT-PiperOrigin-RevId: 946930488
@qukhan

qukhan commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Because #10765 got through and there's a merge conflict, this needs to be rebased again.

… vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.
@destro4evr-rgb
destro4evr-rgb force-pushed the fix/litert-arithmetic-axis-bounds-checks branch from 23c5ae7 to 9fe71df Compare July 17, 2026 09:58
@destro4evr-rgb

destro4evr-rgb commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Hi @qukhan , rebased again onto current master - conflict from #10765 is resolved. CI should be clean. Ready for merge when you get a chance.

copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 17, 2026
--
9fe71df82d9a31e67235f7c05b820d89d41bcd0b by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 9fe71df82d9a31e67235f7c05b820d89d41bcd0b
LiteRT-PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 17, 2026
--
9fe71df82d9a31e67235f7c05b820d89d41bcd0b by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 9fe71df82d9a31e67235f7c05b820d89d41bcd0b
LiteRT-PiperOrigin-RevId: 946930488
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jul 17, 2026
--
9fe71df82d9a31e67235f7c05b820d89d41bcd0b by destro4evr-rgb <destro4evr@proton.me>:

litert/tensor/arithmetic.h: bounds-check axis and index values before vector subscript

Rebased onto master after lvalue LockedBufferSpan refactor.

FUTURE_COPYBARA_INTEGRATE_REVIEW=google/XNNPACK#10702 from destro4evr-rgb:fix/litert-arithmetic-axis-bounds-checks 9fe71df82d9a31e67235f7c05b820d89d41bcd0b
LiteRT-PiperOrigin-RevId: 946930488
@copybara-service
copybara-service Bot merged commit 5251854 into google:master Jul 17, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants